package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Grade;
import cn.edu.ctbu.lddweb.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 年级管理
 */
@Controller
public class GradeController {
    @Autowired
    private GradeService gradeService;

    //后台管理系统
    @RequestMapping("/grade/index")
    public String index(Model model, ModelMap map,Grade grade){
        List<Grade> grades = gradeService.selectList(grade);
        model.addAttribute("grade",grades);
        return "/grade/index";
    }

    //搜索/重置
    @RequestMapping("/selectGrade")
    public String selectGrade(@RequestParam(value = "gradeName",required = false) String gradeName, Model model){
        Grade grade = new Grade();
        grade.setGradeName(gradeName);
        List<Grade> list = gradeService.selectList(grade);
            model.addAttribute("grade",list);
        return "/grade/index";
    }

    //新增页面
    @RequestMapping("/grade/insert")
    public String insert(){
        return "/grade/insert";
    }

    //新增操作
    @RequestMapping("/insertGrade")
    public String insertGrade(Grade grade){
        //需要判断年级名称在数据库中是否存在
        List<Grade> list = gradeService.selectList(grade);
        if(list.size()!=0){
            return "/error111";
        }
        gradeService.insertGrade(grade);
        return "/grade/index";
    }


    //修改
    @RequestMapping("/grade/edit")
    public String edit(int id,Model model){
        Grade grade = gradeService.findID(id);
        model.addAttribute("grade",grade);
        return "/grade/edit";
    }

    //修改操作
    @RequestMapping("/updateGrade")
    public String updateGrade(Grade grade){
        Boolean boo = gradeService.checkSupplierUnique(grade);
        if(boo){
            return "error111";
        }
        gradeService.updateGrade(grade);
        return "/grade/index";
    }

    //删除
    @RequestMapping("/deleteGrade")
    public String deleteGrade(int id){
        gradeService.deleteGrade(String.valueOf(id));
        return "/grade/index";
    }
}
